<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    title="库存初始化"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="20" :sm="24" :xs="24">
          <a-form-item label="初始化位置" v-bind="validateInfos.type">
            <a-select
              optionFilterProp="label"
              :options="options"
              allow-clear
              v-model:value="form.type"
              placeholder="请选择"
              @change="optionsChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label=" " :colon="false">
            <a-select
              optionFilterProp="label"
              :options="options2"
              allow-clear
              v-model:value="form.type_code"
              placeholder="请选择"
              @change="optionsChange2"
              autocomplete="off"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { message, Modal, Form } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import { receipts_purchase_detail } from '@/api/jewelry/receipts_alter_goods';
  import {
    get_warehouse_list,
    get_store_list
  } from '@/api/jewelry/receipts_purchase';
  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  const options = ref([
    { label: '仓库', value: 1 },
    { label: '门店', value: 2 }
  ]);
  const options2 = ref([]);
  // 表单数据
  const form = reactive({
    type: '',
    type_code: ''
  });

  const optionsChange = (e) => {
    form.type = e;
    if (e == 1) {
      getWearhouse();
    } else if (e == 2) {
      getMendian();
    }
  };
  const optionsChange2 = (e) => {
    form.type_code = e;
  };

  const getWearhouse = () => {
    get_warehouse_list({ page: 1, page_size: 99999 }).then((res) => {
      options2.value = [];
      res.list.forEach((item) => {
        options2.value.push({
          label: item.name,
          value: item.code.toString()
        });
      });
    });
  };
  const getMendian = () => {
    get_store_list({ page: 1, page_size: 99999 }).then((res) => {
      options2.value = [];
      res.list.forEach((item) => {
        options2.value.push({
          label: item.name,
          value: item.code.toString()
        });
      });
    });
  };

  // 表单验证规则
  const rules = reactive({
    type: [
      {
        required: true,
        message: '请选择初化位置',
        trigger: 'change'
      }
    ],
    type_code: [
      {
        required: true,
        message: '请选择具体的门店或者仓库',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
